package com.zs.backend.modules.enterpriseOrder.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zs.backend.common.enumeration.OrderAuditingStatus;
import com.zs.backend.common.enumeration.OrderLogType;
import com.zs.backend.common.enumeration.OrderStatus;
import com.zs.backend.modules.enterpriseOrder.dto.EnterpriseOrderDTO;
import com.zs.backend.modules.enterpriseOrder.model.EnterpriseOrder;
import com.zs.backend.common.api.CommonPage;
import com.zs.backend.common.api.CommonResult;
import com.zs.backend.modules.enterpriseOrder.service.EnterpriseOrderService;
import com.zs.backend.modules.orderLog.dto.OrderLogDTO;
import com.zs.backend.modules.orderLog.service.OrderLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.math.BigDecimal;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 企业订单管理表 前端控制器
 * </p>
 *
 * @author zs
 * @since 2024-03-18
 */
@RestController
@Api(tags = "订单-企业部订单、人才部订单、订单打款")
@RequestMapping("/enterpriseOrder")
public class EnterpriseOrderController {

    @Autowired
    private EnterpriseOrderService enterpriseOrderService;
    @Autowired
    private OrderLogService orderLogService;

    @ApiOperation("企业部订单列表、人才部订单列表、订单打款列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<EnterpriseOrderDTO>> list(
        @ApiParam(value = "企业部",name = "enterprise")
        @RequestParam(value = "enterprise", required = false) String enterprise,

        @ApiParam(value = "人才部", name = "personnel")
        @RequestParam(value = "personnel", required = false) String personnel,

        @ApiParam(value = "证书类别(1:注册类；2：非注册类)", name = "type")
        @RequestParam(value = "type", required = false) Integer type,

        @ApiParam(value = "专业", name = "major")
        @RequestParam(value = "major", required = false) String major,

        @ApiParam(value = "去向", name = "destination")
        @RequestParam(value = "destination", required = false) String destination,

        @ApiParam(value = "所属公司名称", name = "companyName")
        @RequestParam(value = "companyName", required = false) String companyName,

        @ApiParam(value = "合同到期时间-开始（yyyy-MM-dd）", name = "startContractEndTime")
        @RequestParam(value = "startContractEndTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startContractEndTime,

        @ApiParam(value = "合同到期时间-结束（yyyy-MM-dd）", name = "endContractEndTime")
        @RequestParam(value = "endContractEndTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endContractEndTime,

        @ApiParam(value = "下单时间-开始（yyyy-MM-dd HH:mm:ss）", name = "startOrderTime")
        @RequestParam(value = "startOrderTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startOrderTime,

        @ApiParam(value = "下单时间-结束（yyyy-MM-dd HH:mm:ss）", name = "endOrderTime")
        @RequestParam(value = "endOrderTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endOrderTime,

        @ApiParam(value = "是否到期（false 否，true 是）", name = "expire")
        @RequestParam(value = "expire", required = false) Boolean expire,

        @ApiParam(value = "人才姓名", name = "personnelName")
        @RequestParam(value = "personnelName", required = false) String personnelName,

        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {

        EnterpriseOrderDTO dto = EnterpriseOrderDTO.builder()
            .enterprise(enterprise)
            .personnel(personnel)
            .type(type)
            .major(major)
            .destination(destination)
            .companyName(companyName)
            .startContractEndTime(startContractEndTime)
            .endContractEndTime(endContractEndTime)
            .startOrderTime(startOrderTime)
            .endOrderTime(endOrderTime)
            .personnelName(personnelName)
            .expire(expire)
            .build();
        Page<EnterpriseOrderDTO> pageList = enterpriseOrderService.list(dto, pageSize,pageNum);
        return CommonResult.success(CommonPage.restPage(pageList));
    }

    @ApiOperation("添加、修改企业订单(form-data 传参)")
    @RequestMapping(value = "/addOrUpt", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult addOrUpt(
        @ApiParam(value = "数据id，修改时必传",name = "id")
        @RequestParam(value = "id", required = false) Long id,
        @ApiParam(value = "企业部",name = "enterprise")
        @RequestParam(value = "enterprise", required = false) String enterprise,

        @ApiParam(value = "人才部", name = "personnel")
        @RequestParam(value = "personnel", required = false) String personnel,

        @ApiParam(value = "证书类别(1:注册类；2：非注册类)", name = "type")
        @RequestParam(value = "type", required = false) Integer type,

        @ApiParam(value = "专业", name = "major")
        @RequestParam(value = "major", required = false) String major,

        @ApiParam(value = "去向", name = "destination")
        @RequestParam(value = "destination", required = false) String destination,

        @ApiParam(value = "所属公司名称", name = "companyName")
        @RequestParam(value = "companyName", required = false) String companyName,

        @ApiParam(value = "收证人", name = "acceptCertificateUserName")
        @RequestParam(value = "acceptCertificateUserName", required = false) String acceptCertificateUserName,

        @ApiParam(value = "对接人电话", name = "contactTel")
        @RequestParam(value = "contactTel", required = false) String contactTel,

        @ApiParam(value = "签合同时间（yyyy-MM-dd）", name = "contractSignTime")
        @RequestParam(value = "contractSignTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date contractSignTime,

        @ApiParam(value = "合同到期时间（yyyy-MM-dd）", name = "contractEndTime")
        @RequestParam(value = "contractEndTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date contractEndTime,

        @ApiParam(value = "服务费", name = "serviceAmount")
        @RequestParam(value = "serviceAmount", required = false) BigDecimal serviceAmount,

        @ApiParam(value = "公司", name = "majorCompany")
        @RequestParam(value = "majorCompany", required = false) String majorCompany,

        @ApiParam(value = "对接人", name = "contactPerson")
        @RequestParam(value = "contactPerson", required = false) String contactPerson,

        @ApiParam(value = "人才姓名", name = "personnelName")
        @RequestParam(value = "personnelName", required = false) String personnelName,

        @RequestParam(value = "paymentImageFile", required = false) MultipartFile paymentImageFile

    ) {
        EnterpriseOrderDTO dto = EnterpriseOrderDTO.builder()
            .enterprise(enterprise)
            .personnel(personnel)
            .type(type)
            .major(major)
            .destination(destination)
            .companyName(companyName)
            .contactPerson(contactPerson)
            .personnelName(personnelName)
            .majorCompany(majorCompany)
            .serviceAmount(serviceAmount)
            .contractEndTime(contractEndTime)
            .contractSignTime(contractSignTime)
            .contactTel(contactTel)
            .acceptCertificateUserName(acceptCertificateUserName)
            .id(id)
            .build();
        Long orderId = enterpriseOrderService.addOrUpt(dto);
        if (id == null) {
            // 保存日志
            OrderLogDTO orderLogDTO = new OrderLogDTO();
            orderLogDTO.setOrderId(orderId);
            orderLogDTO.setDescription(OrderLogType.PAY_BACK.getDesc() + " 提交，服务费金额：" + serviceAmount );
            orderLogDTO.setOperatitonType(OrderLogType.PAY_BACK.getCode());
            orderLogService.saveOrderLog(orderLogDTO);
        }
        return CommonResult.success(null);
    }

    @ApiOperation("打款申请(form-data 传参)")
    @RequestMapping(value = "/payApply", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult payApply(

        @ApiParam(value = "订单id", name = "id")
        @RequestParam(value = "id", required = false) Long id,

        @ApiParam(value = "人才姓名", name = "personnelName")
        @RequestParam(value = "personnelName", required = false) String personnelName,

        @ApiParam(value = "去向", name = "destination")
        @RequestParam(value = "destination", required = false) String destination,

        @ApiParam(value = "打款申请金额", name = "payApplyAmount")
        @RequestParam(value = "payApplyAmount", required = false) BigDecimal payApplyAmount,

        @ApiParam(value = "打款申请-支付类型(从字典项打款申请支付类型获取)", name = "payApplyType")
        @RequestParam(value = "payApplyType", required = false) Integer payApplyType,

        @ApiParam(value = "打款申请-类型(打款)", name = "payApplyApplicationType")
        @RequestParam(value = "payApplyApplicationType", required = false) String payApplyApplicationType,

        @ApiParam(value = "打款申请金额-付款信息", name = "payApplyInfo")
        @RequestParam(value = "payApplyInfo", required = false) String payApplyInfo,

        @ApiParam(value = "打款申请金额-备注", name = "payApplyRemark")
        @RequestParam(value = "payApplyRemark", required = false) String payApplyRemark,

        @RequestParam(value = "paymentImageFile", required = false) MultipartFile paymentImageFile


    ) {
        EnterpriseOrderDTO dto = EnterpriseOrderDTO.builder()
            .personnelName(personnelName)
            .destination(destination)
            .payApplyAmount(payApplyAmount)
            .payApplyType(payApplyType)
            .payApplyApplicationType(payApplyApplicationType)
            .payApplyInfo(payApplyInfo)
            .payApplyRemark(payApplyRemark)
            .paymentTime(new Date())
            .id(id)
            .build();
        Long orderId = enterpriseOrderService.addOrUpt(dto);

        // 保存日志
        OrderLogDTO orderLogDTO = new OrderLogDTO();
        orderLogDTO.setOrderId(dto.getId());
        orderLogDTO.setDescription(OrderLogType.PAY_APPLY.getDesc() + "成功，申请金额：" + payApplyAmount );
        orderLogDTO.setOperatitonType(OrderLogType.PAY_APPLY.getCode());
        orderLogDTO.setAmount(payApplyAmount);
        orderLogService.saveOrderLog(orderLogDTO);
        return CommonResult.success(null);
    }

    @ApiOperation("订单打款审核 (form-data 传参)")
    @RequestMapping(value = "/auditing", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult auditing(

        @ApiParam(value = "订单id", name = "id")
        @RequestParam(value = "id", required = false) Long id,

        @ApiParam(value = "打款证明-图片文件（暂支持单张）", name = "paymentImageFile")
        @RequestParam(value = "paymentImageFile", required = false) MultipartFile paymentImageFile,

        @ApiParam(value = "订单打款审核状态（0：未审核;1：审核通过，2：审核不通过）", name = "auditingStatus")
        @RequestParam(value = "auditingStatus", required = false) Integer auditingStatus
        ) {
        EnterpriseOrderDTO dto = EnterpriseOrderDTO.builder()
            .paymentImageFile(paymentImageFile)
            .id(id)
            .auditingStatus(auditingStatus)
            .build();
        if(OrderAuditingStatus.PASS.getCode().equals(auditingStatus)){
            dto.setStatus(OrderStatus.HAVE_PAY.getCode());
        }
        enterpriseOrderService.addOrUpt(dto);
        // 保存日志
        OrderLogDTO orderLogDTO = new OrderLogDTO();
        orderLogDTO.setOrderId(dto.getId());
        orderLogDTO.setDescription(OrderAuditingStatus.valueOf(auditingStatus).getDesc());
        orderLogDTO.setOperatitonType(OrderLogType.ORDER_PAY.getCode());
        orderLogService.saveOrderLog(orderLogDTO);
        return CommonResult.success(null);
    }


}

